#version 460
#extension GL_GOOGLE_include_directive : enable
#extension GL_EXT_shader_explicit_arithmetic_types_int64 : require
#extension GL_EXT_shader_explicit_arithmetic_types_int16 : require
//#extension GL_EXT_shader_16bit_storage : require


#include "../../../../core/shader_std.h"
#include "../../../../core/shader_built_in_functions.h"
#include "../../../../core/shader_mesh.h"

layout(std140, set = 0, binding = 0) uniform Buf0 {
	mat4 mat_ProjecView[4];
};

layout(std430, set = 1, scalar, column_major, binding = 1) readonly buffer Buf1 {
	mat4 mat_Model[];
};

layout(push_constant, scalar) uniform DRAWPROP_push_constants {
	S_ConstPush_Mesh_Face const_push;
};



layout(location = 0) in vec3 inPos;
layout(location = 1) in vec3 inNormal;
layout(location = 2) in vec3 inUV_Color;


layout(location = 0) out vec4 outColor;
layout(location = 1) out vec3 vNormal;
layout(location = 2) out uint vShaderType;

layout(location = 3) out vec3 vLigth[3];


out gl_PerVertex {
	vec4 gl_Position;
};



void main(void){
	mat4 bonesMat_som = mat_Model[const_push.m_Offset_Tranform + gl_InstanceIndex];
	
	//vColor = f_glsl_buildColor(const_push.m_Ob_Color);
	vNormal = normalize(mat3x3(bonesMat_som) * inNormal);
	//vShaderType = const_push.m_Ob_ShaderType;
	vShaderType = const_push.m_Ob_ShaderType&0xff;
	

	switch ( vShaderType ) {
		case DEF_3DShaderType_FronWeight :
		case DEF_3DShaderType_PerObColor : {
			outColor = f_glsl_buildColor(const_push.m_Ob_Color);
			break;
		}
		case DEF_3DShaderType_VertAddObColor : {
			outColor = f_glsl_buildColor(floatBitsToUint(inUV_Color.z)) + f_glsl_buildColor(const_push.m_Ob_Color);
			break;
		}
		default:{
			outColor = f_glsl_buildColor(floatBitsToUint(inUV_Color.z));
		}
	}


	vec4 p = bonesMat_som * vec4(inPos, 1);
	//vec3 camPos = (inverse(mat_ProjecView[3]) * vec4(0,0,0,1)).xyz;
	gl_Position = mat_ProjecView[DEF_Camera_Matrix_PV] * p;
	
	//gl_Position.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy / gl_Position.w;
	//gl_Position.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy / gl_Position.w;

	vec4 ndcpos = gl_Position / gl_Position.w;
	ndcpos.xy += mat_ProjecView[DEF_Camera_Matrix_Porj][0].xy;
	gl_Position = ndcpos * gl_Position.w;


	mat3 ligthMat = inverse(mat3(mat_ProjecView[DEF_Camera_Matrix_Rot]));
	vLigth[0] = ligthMat * vec3(-1, 1, -1);
	vLigth[1] = ligthMat * vec3(1, 0.5, -0.3);
	vLigth[2] = ligthMat * vec3(0.5, -0.6, -0.5);

}


 

